/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.classes;

import br.com.fatecpg.mdi.Conexão;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AnimalAcessaDados {
    private Conexão conexao = new Conexão();
    private Connection conn;
    private PreparedStatement pstmt;
    
    
    public boolean incluir(ModeloAnimal a) throws SQLException {
        String insertSql = "INSERT INTO animal(TIPO_ANIMAL_id_animal, nm_animal, ds_peso_animal, qt_idade_animal, nm_raca) " +
                "VALUES(?, ?, ?, ?, ?)";
        try {
            conn = conexao.getConexao();
            //conn = conexao.getConectar;
            pstmt = conn.prepareStatement(insertSql);
            pstmt.setInt(1, a.getId());
            pstmt.setString(2, a.getNome());
            pstmt.setFloat(3, (float)a.getPeso());
            pstmt.setInt(4, a.getIdade());
            pstmt.setString(5, a.getNomeRaca());
            
            return pstmt.execute();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
        }
    }
    
    public int alterar(ModeloAnimal a) throws SQLException {
        String updateSql = "UPDATE animal SET TIPO_ANIMAL_id_animal = ?, nm_animal = ?, ds_peso_animal = ?, qt_idade_animal = ?, nm_raca = ? " +
               "WHERE id_animal = ?";
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(updateSql);
            pstmt.setInt(1, a.getTipoAnimalId());
            pstmt.setString(2, a.getNome());
            pstmt.setFloat(3, (float)a.getPeso());
            pstmt.setInt(4, a.getIdade());
            pstmt.setString(5, a.getNomeRaca());
            pstmt.setInt(6, a.getId());
            
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
        }
    }
    
    public ArrayList<ModeloAnimal> consulta(String filtro) throws SQLException{
        ArrayList<ModeloAnimal> lista = new ArrayList<ModeloAnimal>();
        String selectSql = "SELECT * FROM animal WHERE " + filtro;
        ResultSet retornoConsulta = null;
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(selectSql);
            retornoConsulta = pstmt.executeQuery();
            
            while (retornoConsulta.next()) {
                ModeloAnimal a = new ModeloAnimal();
                a.setId(retornoConsulta.getInt(1));
                a.setTipoAnimalId(retornoConsulta.getInt(2));
                a.setNome(retornoConsulta.getString(3));
                a.setPeso(retornoConsulta.getFloat(4));
                a.setIdade(retornoConsulta.getInt(5));
                a.setNomeRaca(retornoConsulta.getString(6));
                
                
                lista.add(a);
            }
            
            return lista;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (retornoConsulta != null) {
                retornoConsulta.close();
            }
            conexao.fecharConexao();
           
        }
    }
    
    public ArrayList<ModeloAnimal> consultaTodos() throws SQLException{
        ArrayList<ModeloAnimal> lista = new ArrayList<ModeloAnimal>();
        String selectSql = "SELECT * FROM animal";
        ResultSet retornoConsulta = null;
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(selectSql);
            retornoConsulta = pstmt.executeQuery();
            
            while (retornoConsulta.next()) {
                ModeloAnimal a = new ModeloAnimal();
                a.setId(retornoConsulta.getInt(1));
                a.setTipoAnimalId(retornoConsulta.getInt(2));
                a.setNome(retornoConsulta.getString(3));
                a.setPeso(retornoConsulta.getFloat(4));
                a.setIdade(retornoConsulta.getInt(5));
                a.setNomeRaca(retornoConsulta.getString(6));
                
                
                lista.add(a);
            }
            
            return lista;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (retornoConsulta != null) {
                retornoConsulta.close();
            }
            conexao.fecharConexao();
           
        }
    }
    
     public int consultaMaxId() throws SQLException {
        String selectSql = "SELECT MAX(id_anima) FROM animal";
        ResultSet retornoConsulta = null;
        int maxID = 0;
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(selectSql);
            retornoConsulta = pstmt.executeQuery();
            
            if (retornoConsulta.next()) {
                maxID = retornoConsulta.getInt(1);
            }
            
            return maxID;
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (retornoConsulta != null) {
                retornoConsulta.close();
            }
            conexao.fecharConexao();
           
        }
    }
    
    public int deletar(int id) throws SQLException {
        String deleteSql = "DELETE FROM animal WHERE id_animal = ?";
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(deleteSql);
            pstmt.setInt(1, id);
            
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
           
        }
    }
}
